<template>
  <el-dialog title="编辑部门" :visible.sync="dialogVisible" width="30%" :append-to-body="true">
    <el-form ref="editForm" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" v-model="form.note"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit('editForm')">保存</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { baseUrl } from "@/config/env";
const urls = {
  checkField: baseUrl + "/zweixin/common/check_unique_notme", // 验证编码唯一性
  save: baseUrl + "/zweixin/sys/depart/edit", // 保存
  get: baseUrl + "/zweixin/sys/depart/get"
};
export default {
  data() {
    return {
      dialogVisible: false,
      super_depart: {
        code: ""
      },
      form: {
        kid: null,
        name: null,
        code: null,
        note: null
      },
      rules: {
        name: [{ required: true, message: "请输入部门名称", trigger: "blur" }],
        code: [
          { required: true, message: "请输入部门编码", trigger: "change" },
          { min: 3, max: 3, message: "长度必须为3个字符", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              let code = this.super_depart.code + value;
              axios
                .get(urls.checkField, {
                  params: {
                    table: "t_depart",
                    field: "code",
                    value: code,
                    kid: this.form.kid
                  }
                })
                .then(res => {
                  if (res.data.result) {
                    callback("部门编码必须唯一");
                  } else {
                    callback();
                  }
                });
            },
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    show(kid) {
      this.dialogVisible = true;
      if (this.$refs["editForm"]) {
        this.$refs["editForm"].resetFields();
      }
      this.fetch(kid);
    },
    onSubmit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.form.code = this.super_depart.code + this.form.code;
          axios.post(urls.save, this.form).then(res => {
            this.$message(res.data.result);
            this.dialogVisible = false;
            this.$emit("refresh");
          });
        }
        return false;
      });
    },
    fetch(kid) {
      axios.get(urls.get, { params: { kid: kid } }).then(res => {
        let data = res.data.result;
        this.super_depart.code = data.code.substring(0, data.code.length - 3);
        this.form = {
          kid: data.kid,
          name: data.name,
          code: data.code.substring(data.code.length - 3, data.code.length),
          url: data.url,
          icon: data.icon,
          type: data.type,
          note: data.note
        };
      });
    }
  }
};
</script>